package com.totoro.task;

import com.totoro.algorithm.DifferenceHashAlgorithm;
import com.totoro.algorithm.PerceptualHashAlgorithm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

@Component
public class SchedulerTask {

    private final Logger LOGGER = LoggerFactory.getLogger(this.getClass());
    private int count = 0;
    private static String dir = "/Volumes/test/fileDown/images/";

    private static PerceptualHashAlgorithm p = new PerceptualHashAlgorithm();
    @Scheduled(cron = "*/6 * * * * ?")
    @Scheduled(fixedRate = 1000 * 60 * 10)
    private void process() {

        iteratorPath(dir);

        /*System.out.println("this is scheduler task runing  " + (count++));
        PerceptualHashAlgorithm p = new PerceptualHashAlgorithm();
        String imagePath = "/Users/swy/Desktop/";
        LOGGER.info("图1 vs 图2 ：{}", p.imgChk(imagePath + "1.jpeg", imagePath + "2.jpeg", 10));
        LOGGER.info("图1 vs 图3 ：{}", p.imgChk(imagePath + "1.jpeg", imagePath + "3.jpeg", 10));
        LOGGER.info("图1 vs 图4 ：{}", p.imgChk(imagePath + "1.jpeg", imagePath + "4.jpeg", 10));
        LOGGER.info("图1 vs 图5 ：{}", p.imgChk(imagePath + "1.jpeg", imagePath + "5.jpeg", 10));
        LOGGER.info("图1 vs 图6 ：{}", p.imgChk(imagePath + "1.jpeg", imagePath + "6.jpeg", 10));
        LOGGER.info("图1 vs 图7 ：{}", p.imgChk(imagePath + "1.jpeg", imagePath + "7.jpeg", 10));
        LOGGER.info("图2 vs 图3 ：{}", p.imgChk(imagePath + "2.jpeg", imagePath + "3.jpeg", 10));
        LOGGER.info("图1 vs 图8 ：{}", p.imgChk(imagePath + "1.jpeg", imagePath + "8.jpeg", 10));
        LOGGER.info("图1 vs 图9 ：{}", p.imgChk(imagePath + "1.jpeg", imagePath + "9.jpeg", 20));
        LOGGER.info("图1 vs 图10 ：{}", p.imgChk(imagePath + "1.jpeg", imagePath + "10.jpeg", 10));
        LOGGER.info("图1 vs 图11 ：{}", p.imgChk(imagePath + "1.jpeg", imagePath + "11.jpeg", 10));
        LOGGER.info("图1 vs 图12 ：{}", p.imgChk(imagePath + "1.jpeg", imagePath + "12.jpeg", 10));
        LOGGER.info("图1 vs 图66 ：{}", p.imgChk(imagePath + "66.jpeg", imagePath + "66.jpeg", 10));*/

        //LOGGER.info("图1 vs 图2 ：{}", DifferenceHashAlgorithm.imgChk(imagePath + "1.jpeg", imagePath + "2.jpeg", 10));

        /*LOGGER.info("图1 vs 图2 ：{}", DifferenceHashAlgorithm.imgChk(imagePath + "1.jpeg", imagePath + "2.jpeg", 10));
        LOGGER.info("图1 vs 图3 ：{}", DifferenceHashAlgorithm.imgChk(imagePath + "1.jpeg", imagePath + "3.jpeg", 10));
        LOGGER.info("图1 vs 图4 ：{}", DifferenceHashAlgorithm.imgChk(imagePath + "1.jpeg", imagePath + "4.jpeg", 10));
        LOGGER.info("图1 vs 图5 ：{}", DifferenceHashAlgorithm.imgChk(imagePath + "1.jpeg", imagePath + "5.jpeg", 10));
        LOGGER.info("图1 vs 图6 ：{}", DifferenceHashAlgorithm.imgChk(imagePath + "1.jpeg", imagePath + "6.jpeg", 10));
        LOGGER.info("图1 vs 图7 ：{}", DifferenceHashAlgorithm.imgChk(imagePath + "1.jpeg", imagePath + "7.jpeg", 10));
        LOGGER.info("图2 vs 图3 ：{}", DifferenceHashAlgorithm.imgChk(imagePath + "2.jpeg", imagePath + "3.jpeg", 10));
        LOGGER.info("图1 vs 图8 ：{}", DifferenceHashAlgorithm.imgChk(imagePath + "1.jpeg", imagePath + "8.jpeg", 10));
        LOGGER.info("图1 vs 图9 ：{}", DifferenceHashAlgorithm.imgChk(imagePath + "1.jpeg", imagePath + "9.jpeg", 20));
        LOGGER.info("图1 vs 图10 ：{}", DifferenceHashAlgorithm.imgChk(imagePath + "1.jpeg", imagePath + "10.jpeg", 10));
        LOGGER.info("图1 vs 图11 ：{}", DifferenceHashAlgorithm.imgChk(imagePath + "1.jpeg", imagePath + "11.jpeg", 10));
        LOGGER.info("图1 vs 图12 ：{}", DifferenceHashAlgorithm.imgChk(imagePath + "1.jpeg", imagePath + "12.jpeg", 10));
        LOGGER.info("图1 vs 图66 ：{}", DifferenceHashAlgorithm.imgChk(imagePath + "66.jpeg", imagePath + "66.jpeg", 10));*/

    }

    File or;
    File[] files;

    // 用于遍历文件价
    public void iteratorPath(String dir) {
        or = new File(dir);
        files = or.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    if (!file.getName().contains(".DS_Store")) {
                        try {
                            System.out.println(p.getHash(new FileInputStream(file)));
                        }catch (Exception e){
                            System.out.println(e);
                        }
                    }
                } else if (file.isDirectory()) {
                    iteratorPath(file.getAbsolutePath());
                }
            }
        }
    }
}